#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: send_test_data_to_beanstalk.py
@time: 2018/1/10 10:38
"""

import sys

sys.path.append('..')
sys.path.append('../..')
from common.pybeanstalk import PyBeanstalk
from config.beanstalk_conf import BEANSTALK_CONF

from common.mongo import MongDb
from config.mongo_conf import MONGO_DB_SOURCE

# 日志模块
from logger import Logger

log = Logger('send_to_beanstalk.log').get_logger()

APP_DATA_DB_SOURCE = {
    'host': '172.16.215.16',
    'port': 40042,
    'db': 'app_data',
    'username': 'work',
    'password': 'haizhi'
}

source_db = MongDb(MONGO_DB_SOURCE['host'], MONGO_DB_SOURCE['port'], MONGO_DB_SOURCE['db'],
                   MONGO_DB_SOURCE['username'],
                   MONGO_DB_SOURCE['password'], log=log)
app_data_db = MongDb(APP_DATA_DB_SOURCE['host'], APP_DATA_DB_SOURCE['port'], APP_DATA_DB_SOURCE['db'],
                     APP_DATA_DB_SOURCE['username'],
                     APP_DATA_DB_SOURCE['password'], log=log)

# 初始化监听消息队列
beanstalk = PyBeanstalk(BEANSTALK_CONF['host'], BEANSTALK_CONF['port'])


def main():
    seed_table = 'new_gansu_company_list'
    target_table = 'enterprise_data_gov_new_2018_01_08'
    target_table1 = 'enterprise_data_gov'
    with open('gansu.txt', 'w') as p_file:
        for item in source_db.traverse_batch_field(seed_table, {'crawl_status': 1}, ['_id']):
            company = item.get('_id')
            if app_data_db.find_one(target_table, {'company': company}) is not None:
                if app_data_db.find_one(target_table1, {'company': company}) is not None:
                    p_file.write(company + '\n')

    log.info("数据发送完成...")


if __name__ == '__main__':
    main()
